package com.whq.knowverywell.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.whq.knowverywell.R;
import com.whq.knowverywell.activity.ContentFragment.OnSumDataListener;
import com.whq.knowverywell.adapter.ProductSearchAdapter;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.constant.ParamUtil;

/**
 * product search (because the component of the page is different from the
 * others, it is not suitable for fragment)
 * 
 * @author daiwei
 * 
 */
public class ProductSearchActivity extends BaseLoginActivity implements ActionBar.OnNavigationListener, OnSumDataListener{
	private static final String TAG = ProductSearchActivity.class
			.getSimpleName();

	private ListView mListView;
	private EditText mSearchEditext;
	private ImageView mSearchImageView, mScanImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		LogUtil.d(TAG, "onCreate start ...");
		setTheme(R.style.Theme_Sherlock_Light);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_search);

		mSumTextView = (TextView) findViewById(R.id.sum_textview);
		mAmountTextView = (TextView) findViewById(R.id.amount_textview);
		mSearchImageView = (ImageView) findViewById(R.id.search_imageview);
		mSearchImageView.setOnClickListener(this);
		mScanImageView = (ImageView) findViewById(R.id.scan_imageview);
		mScanImageView.setOnClickListener(this);
		// TODO add suggestion
		mSearchEditext = (EditText) findViewById(R.id.search_editext);
		mSearchEditext.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
		mListView = (ListView) findViewById(R.id.search_result_listview);
		mListView.setCacheColorHint(-000);
		//mListView.setSelector(-000);
		ProductSearchAdapter adapter = new ProductSearchAdapter(this, new ArrayList(4));
		mListView.setAdapter(adapter);
		
		getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.top_menu_bg));
		//getSupportActionBar().setIcon(R.drawable.home_btn_n);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		
		Context context = getSupportActionBar().getThemedContext();
		ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.homeitems, R.layout.sherlock_spinner_item);
		list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
		
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		getSupportActionBar().setListNavigationCallbacks(list, (OnNavigationListener) this);
	}

	@Override
	public void onClick(View v) {
		LogUtil.d(TAG, "onClick view id[" + v.getId() + "}");
		super.onClick(v);

		switch (v.getId()) {
		case R.id.search_imageview:

			break;

		case R.id.scan_imageview:

			break;

		default:
			break;
		}
	}
	
	@Override
	protected void getParameters() {
		super.getParameters();
		Intent intent = getIntent();
		if (intent != null) {
			itemId = intent.getIntExtra(ParamUtil.BUNDLE_KEY_ITEM_ID, -1);
		}
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SubMenu sub = menu.addSubMenu("设置");
        sub.setIcon(R.drawable.btn_set);
        sub.add(0, R.style.Theme_Sherlock, 0, "注销");
        sub.add(0, R.style.Theme_Sherlock_Light, 0, "管理帐号");
        sub.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0, "关于");
        sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home || item.getItemId() == 0) {
            return false;
        }
        Toast.makeText(this, "Theme changed to \"" + item.getTitle() + "\"", Toast.LENGTH_SHORT).show();
        return true;
    }

	@Override
	public void onSumData(long totalSalesVolume, double totalSalesAmount) {
		
	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		Toast.makeText(this, "Selected: " + itemTitles[itemPosition], Toast.LENGTH_SHORT).show();
		//mSelectedTextView.setText("Selected: " + itemTitles[itemPosition]);
		return true;
	}
	
}
